package com.auto.service.impl.coder.tree.java;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.auto.annotation.AutoCode;
import com.auto.annotation.Column;
import com.auto.annotation.MyFunction;
import com.auto.annotation.Table;
import com.auto.service.impl.BaseCoder;
import com.auto.service.impl.Coder;
import com.common.ArrayUtils;
import com.common.CommonUtil;
import com.common.Global;

/**
 * @author chenfuqiang
 *
 */
public class MapperCoder extends BaseCoder implements Coder {

	private final String CLASSTAIL = "Mapper.xml";
	private final String TEMPLATE_PATH = "/tree/java/Mapper.ftl";


	@Override
	public Map<String, Object> getRoot(Class<?> pojo) {
		Map<String,Object> root = new HashMap<String, Object>();
		root.put("package", Global.MAPPING_PACKAGE);
		root.put("beanPackage", Global.PACKAGEOFTARGETPOJO);
		root.put("daoPackage", Global.DAO_PACKAGE);
		root.put("servicePackage", Global.SERVICE_PACKAGE);
		root.put("author", Global.AUTHOR);
		root.put("className", pojo.getSimpleName());
		root.put("paramName", pojo.getSimpleName().substring(0,1).toLowerCase()+pojo.getSimpleName().substring(1));
		String tableName = pojo.getAnnotation(Table.class).name();
		if(CommonUtil.isEmpty(tableName)) {
			tableName = pojo.getSimpleName();
		}

		if(tableName.toUpperCase().indexOf("@YEAR")!=-1) {
			root.put("yearTable", true);
		}else {
			root.put("yearTable", false);
		}
		tableName = tableName.toUpperCase().replace("@YEAR", "");
		root.put("tableName", tableName);
		//功能扩展
		MyFunction myFunction = pojo.getAnnotation(MyFunction.class);
		if(myFunction != null) {
			root.put("autoInitFromAccess", myFunction.autoInitFromAccess());
			root.put("copyLastYearData", myFunction.copyLastYearData());
			root.put("treeSort", myFunction.treeSort());
			root.put("importData", myFunction.importData());
		}else {
			root.put("autoInitFromAccess", false);
			root.put("copyLastYearData", false);
			root.put("treeSort", false);
			root.put("importData", false);
		}
		Field[] fields = pojo.getDeclaredFields();
		//是否需要父类的字段。
		AutoCode ac = pojo.getAnnotation(AutoCode.class);
		if(ac.includeSupperClass()) {
			if(pojo.getSuperclass()!=null){
				Class<?> clsSup = pojo.getSuperclass();
				fields = (Field[]) ArrayUtils.addAll(fields,clsSup.getDeclaredFields());
			}
		}

		StringBuffer fieldStr = new StringBuffer();//store like  id,username,age,...
		StringBuffer addValueStr = new StringBuffer();
		StringBuffer addFieldStr = new StringBuffer();
		List<Map<String,String>> primaryList = new ArrayList<Map<String,String>>();
		List<Map<String,String>> normalList = new ArrayList<Map<String,String>>();
		List<Map<String,String>> updateList = new ArrayList<Map<String,String>>();
		List<String> keyWordFiltes = new ArrayList<String>();
		String key = null;
		String leaf = null;
		String parentId = null;
		String treecolumnDB = null;
		for(Field field : fields) {
			Column column = field.getAnnotation(Column.class);
			if(column == null) {
				continue;
			}
			String dbcname = CommonUtil.isEmpty(column.name())?field.getName():column.name();
			String proper = field.getName();
			String flag = CommonUtil.isEmpty(column.flag())?"NORMAL":column.flag().toUpperCase();
			String type = CommonUtil.isEmpty(column.type())?"VARCHAR":column.type().toUpperCase();
			if(column.keyWordFilte()) {
				keyWordFiltes.add(dbcname);
			}
			if(column.treeId()) {
				key = dbcname;
			}
			if(column.treeleaf()) {
				leaf = dbcname;
			}
			if(column.treeparentId()) {
				parentId = dbcname;
			}
			if(type.indexOf("VARCHAR")!=-1) {
				type="VARCHAR";
			}else if(type.indexOf("INT")!=-1) {
				type="INTEGER";
			}else if(type.indexOf("DECIMAL")!=-1) {
				type="DECIMAL";
			}
			if(fieldStr.length()==0) {
				fieldStr.append(dbcname);
			}else {
				fieldStr.append(","+dbcname);
			}
			if(column.treecolumn()) {
				treecolumnDB = dbcname;
			}
			if(!column.oth().toUpperCase().equals("IDENTITY(1,1)")) {
				if(addFieldStr.length()==0) {
					addFieldStr.append(dbcname);
					addValueStr.append("#{"+proper+"}");
				}else {
					addFieldStr.append(","+dbcname);
					addValueStr.append(",#{"+proper+"}");
				}
			}
			Map<String,String> map = new HashMap<String, String>();
			map.put("dbcname", dbcname);
			map.put("proper", proper);
			map.put("type", type);
			if(flag.equals("PRIMARY")) {
				primaryList.add(map);
			}else {
				normalList.add(map);
			}
			if(!CommonUtil.isEmpty(column.jsname())) {
				updateList.add(map);
			}
		}
		root.put("key", key);
		root.put("treecolumnDB", treecolumnDB);
		root.put("getKey", "get"+key.substring(0,1).toUpperCase()+key.substring(1));
		root.put("leaf", leaf);
		root.put("getLeaf", "get"+leaf.substring(0,1).toUpperCase()+leaf.substring(1));
		root.put("setLeaf", "set"+leaf.substring(0,1).toUpperCase()+leaf.substring(1));
		root.put("parentId", parentId);
		root.put("getParentId", "get"+parentId.substring(0,1).toUpperCase()+parentId.substring(1));
		root.put("setParentId", "set"+parentId.substring(0,1).toUpperCase()+parentId.substring(1));
		root.put("primaryList", primaryList);
		root.put("normalList", normalList);
		root.put("updateList", updateList);
		root.put("keyWordFiltes",keyWordFiltes);
		root.put("columnFields",fieldStr.toString());
		root.put("addColumnFields",addFieldStr.toString());
		root.put("addValueFields",addValueStr.toString());

		return root;
	}

	@Override
	public String getTargetPackage() {
		return "";
	}

	@Override
	public String getSrcPath() {
		return Global.MAPPING_PACKAGE;
	}

	@Override
	public String getFtlPath() {
		return this.TEMPLATE_PATH;
	}

	@Override
	public String getClassTail() {
		return this.CLASSTAIL;
	}



}
